package com.small.xx.ai.apidoc.controller;

import com.small.xx.ai.apidoc.dto.ApiInfoRequest;
import com.small.xx.ai.apidoc.dto.ApiInfoResponse;
import com.small.xx.ai.apidoc.service.ApiInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * API信息REST控制器
 */
@RestController
@RequestMapping("/api/v1/api-info")
public class ApiInfoController {
    
    @Autowired
    private ApiInfoService apiInfoService;
    
    /**
     * 创建API信息
     * 
     * @param request API信息请求
     * @return API信息响应
     */
    @PostMapping
    public ResponseEntity<ApiInfoResponse> createApi(@RequestBody ApiInfoRequest request) {
        try {
            ApiInfoResponse response = apiInfoService.createApi(request);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }
    
    /**
     * 根据ID获取API信息
     * 
     * @param id API ID
     * @return API信息响应
     */
    @GetMapping("/{id}")
    public ResponseEntity<ApiInfoResponse> getApiById(@PathVariable(name = "id") Long id) {
        ApiInfoResponse response = apiInfoService.getApiById(id);
        if (response == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(response);
    }
    
    /**
     * 获取所有API信息
     * 
     * @return API信息响应列表
     */
    @GetMapping
    public ResponseEntity<List<ApiInfoResponse>> getAllApis() {
        List<ApiInfoResponse> apis = apiInfoService.getAllApis();
        return ResponseEntity.ok(apis);
    }
    
    /**
     * 更新API信息
     * 
     * @param id API ID
     * @param request API信息请求
     * @return API信息响应
     */
    @PutMapping("/{id}")
    public ResponseEntity<ApiInfoResponse> updateApi(@PathVariable(name = "id") Long id, @RequestBody ApiInfoRequest request) {
        ApiInfoResponse response = apiInfoService.updateApi(id, request);
        if (response == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(response);
    }
    
    /**
     * 删除API信息
     * 
     * @param id API ID
     * @return 是否删除成功
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteApi(@PathVariable(name = "id") Long id) {
        boolean deleted = apiInfoService.deleteApi(id);
        if (!deleted) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok().build();
    }
    
    /**
     * 测试API接口
     * 
     * @param id API ID
     * @param testParams 测试参数
     * @return 测试结果
     */
    @PostMapping("/{id}/test")
    public ResponseEntity<String> testApi(@PathVariable(name = "id") Long id, @RequestParam String testParams) {
        try {
            String result = apiInfoService.testApi(id, testParams);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("测试失败: " + e.getMessage());
        }
    }
}